package es.udc.emarketapp.http.util.struts.action;

import es.udc.fbellas.j2ee.util.struts.action.PropertyValidator;
import java.util.Calendar;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;

public class EMarketPropertyValidator {

    private final static String INCORRECT_VALUE =
            "ErrorMessages.incorrectValue";

    private EMarketPropertyValidator() {
    }

    public final static boolean validateBirthDate(ActionErrors errors,
            String propertyNameYear, Long a, String propertyNameMonth, Long m,
            String propertyNameDay, Long d) {

        boolean ret = false;
        if ((a <= Calendar.getInstance().get(Calendar.YEAR) - 18
                && a > Calendar.getInstance().get(Calendar.YEAR) - 138)) {
            if ((m <= 12 && m >= 1)) {
                if (((m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8) || (m == 10) || (m == 12))
                        && (d >= 1) && (d <= 31)) {
                    ret = true;
                } else if (((m == 4) || (m == 6) || (m == 9) || (m == 11)) && (d >= 1) && (d <= 30)) {
                    ret = true;
                } else if (((m == 2) && (a % 4 == 0) && (d >= 1) && (d <= 29))
                        || ((m == 2) && (a % 4 != 0) && (d >= 1) && (d <= 28))) {
                    ret = true;
                } else {
                    errors.add(propertyNameDay, new ActionMessage(INCORRECT_VALUE));
                }
            } else {
                errors.add(propertyNameMonth, new ActionMessage(INCORRECT_VALUE));
            }
        } else {
            errors.add(propertyNameYear, new ActionMessage(INCORRECT_VALUE));
            if (m > 12 || m < 1) {
                errors.add(propertyNameMonth, new ActionMessage(INCORRECT_VALUE));
            }
        }

        return ret;
    }

    public final static boolean validateDate(ActionErrors errors,
            String propertyNameYear, Long a, String propertyNameMonth, Long m,
            String propertyNameDay, Long d) {

        boolean ret = false;
        if ((a < Calendar.getInstance().getMaximum(Calendar.YEAR)
                && a > Calendar.getInstance().getMinimum(Calendar.YEAR))) {
            if ((m <= 12 && m >= 1)) {
                if (((m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8) || (m == 10) || (m == 12))
                        && (d >= 1) && (d <= 31)) {
                    ret = true;
                } else if (((m == 4) || (m == 6) || (m == 9) || (m == 11)) && (d >= 1) && (d <= 30)) {
                    ret = true;
                } else if (((m == 2) && (a % 4 == 0) && (d >= 1) && (d <= 29))
                        || ((m == 2) && (a % 4 != 0) && (d >= 1) && (d <= 28))) {
                    ret = true;
                } else {
                    errors.add(propertyNameDay, new ActionMessage(INCORRECT_VALUE));
                }
            } else {
                errors.add(propertyNameMonth, new ActionMessage(INCORRECT_VALUE));
            }
        } else {
            errors.add(propertyNameYear, new ActionMessage(INCORRECT_VALUE));
            if (m > 12 || m < 1) {
                errors.add(propertyNameMonth, new ActionMessage(INCORRECT_VALUE));
            }
        }

        return ret;
    }

    public final static void validateEmailAddress(ActionErrors errors,
            String propertyName, String propertyValue) {

        if (PropertyValidator.validateMandatory(errors, propertyName,
                propertyValue)) {

            /**
             * Check that "propertyValue" contains the sign "@" surrounded by
             * at least one character.
             */
            int atSignIndex = propertyValue.indexOf("@");

            if ((atSignIndex < 1)
                    || (atSignIndex == propertyValue.length() - 1)) {
                errors.add(propertyName,
                        new ActionMessage(INCORRECT_VALUE));
            }

        }

    }



     public final static void validateString(ActionErrors errors,
        String propertyName, String propertyValue, int length,
        boolean mandatory) {

        if (validateMandatory(errors, propertyName, propertyValue,
            mandatory)) {

            if (propertyValue.length() < length) {
                errors.add(propertyName, new ActionMessage(INCORRECT_VALUE));
            }

        }

    }


        private final static boolean validateMandatory(ActionErrors errors,
        String propertyName, String propertyValue, boolean mandatory) {

        if (mandatory) {
            return PropertyValidator.validateMandatory(errors, propertyName, propertyValue);
        } else {
            return true;
        }

    }
}
